package heven.holt.eyepetizer.adapter.holder

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import heven.holt.eyepetizer.databinding.ItemFollowCardTypeBinding
import heven.holt.eyepetizer.databinding.ItemSpecialSquareCardCollectionTypeBinding
import heven.holt.eyepetizer.databinding.ItemTextCardTypeHeaderFiveBinding
import heven.holt.eyepetizer.databinding.ItemVideoSmallCardTypeBinding
import heven.holt.eyepetizer.model.BasePageModel

object CommendViewHolder {
    private const val UNKNOWN = -1

    private const val CUSTOM_HEADER = 0         //自定义头部类型。

    private const val TEXT_CARD_HEADER1 = 1

    private const val TEXT_CARD_HEADER2 = 2

    private const val TEXT_CARD_HEADER3 = 3

    private const val TEXT_CARD_HEADER4 = 4     //type:textCard -> dataType:TextCard,type:header4

    private val TEXT_CARD_HEADER5 = 5     //type:textCard -> dataType:TextCard -> type:header5

    private const val TEXT_CARD_HEADER6 = 6

    private const val TEXT_CARD_HEADER7 =
        7    //type:textCard -> dataType:TextCardWithRightAndLeftTitle,type:header7

    private const val TEXT_CARD_HEADER8 =
        8    //type:textCard -> dataType:TextCardWithRightAndLeftTitle,type:header8

    private const val TEXT_CARD_FOOTER1 = 9

    private const val TEXT_CARD_FOOTER2 = 10    //type:textCard -> dataType:TextCard,type:footer2

    private const val TEXT_CARD_FOOTER3 =
        11    //type:textCard -> dataType:TextCardWithTagId,type:footer3

    private const val BANNER = 12               //type:banner -> dataType:Banner

    private const val BANNER3 = 13              //type:banner3-> dataType:Banner

    private const val FOLLOW_CARD =
        14          //type:followCard -> dataType:FollowCard -> type:video -> dataType:VideoBeanForClient

    private const val TAG_BRIEFCARD = 15        //type:briefCard -> dataType:TagBriefCard

    private const val TOPIC_BRIEFCARD = 16      //type:briefCard -> dataType:TopicBriefCard

    private const val COLUMN_CARD_LIST = 17      //type:columnCardList -> dataType:ItemCollection

    private const val VIDEO_SMALL_CARD = 18     //type:videoSmallCard -> dataType:VideoBeanForClient

    private const val INFORMATION_CARD = 19     //type:informationCard -> dataType:InformationCard

    private const val AUTO_PLAY_VIDEO_AD =
        20   //type:autoPlayVideoAd -> dataType:AutoPlayVideoAdDetail

    private const val HORIZONTAL_SCROLL_CARD =
        21    //type:horizontalScrollCard -> dataType:HorizontalScrollCard

    private const val SPECIAL_SQUARE_CARD_COLLECTION =
        22   //type:specialSquareCardCollection -> dataType:ItemCollection

    private const val UGC_SELECTED_CARD_COLLECTION =
        23   //type:ugcSelectedCardCollection -> dataType:ItemCollection

    private const val MAX = 100   //避免外部其他类型与此处包含的某个类型重复。

    fun getViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): RecyclerView.ViewHolder = when (viewType) {
        TEXT_CARD_HEADER5 -> {
            TextCardViewHeader5ViewHolder(
                ItemTextCardTypeHeaderFiveBinding.inflate(
                    LayoutInflater.from(
                        parent.context
                    ), parent, false
                )
            )
        }
        SPECIAL_SQUARE_CARD_COLLECTION -> {
            SpecialSquareCardCollectionViewHolder(
                ItemSpecialSquareCardCollectionTypeBinding.inflate(
                    LayoutInflater.from(
                        parent.context
                    ), parent, false
                )
            )
        }
        FOLLOW_CARD -> {
            FollowCardViewHolder(
                ItemFollowCardTypeBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        }
        VIDEO_SMALL_CARD -> {
            VideoSmallCardViewHolder(
                ItemVideoSmallCardTypeBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        }
        else -> EmptyViewHolder(View(parent.context))
    }

    fun getItemViewType(item: BasePageModel.Item): Int {
        return if (item.type == "textCard")
            getTextCardType(item.data?.type)
        else
            getItemViewType(
                item.type,
                item.data?.dataType
            )
    }

    private fun getItemViewType(type: String?, dataType: String?): Int = when (type) {

        "horizontalScrollCard" -> {
            when (dataType) {
                "HorizontalScrollCard" -> HORIZONTAL_SCROLL_CARD
                else -> UNKNOWN
            }
        }
        "squareCardCollection", "specialSquareCardCollection" -> {
            when (dataType) {
                "ItemCollection" -> SPECIAL_SQUARE_CARD_COLLECTION
                else -> UNKNOWN
            }
        }
        "columnCardList" -> {
            when (dataType) {
                "ItemCollection" -> COLUMN_CARD_LIST
                else -> UNKNOWN
            }
        }
//        "textCard" -> {
//            when (dataType) {
//                "header5" -> TEXT_CARD_HEADER5
//                "header7" -> TEXT_CARD_HEADER7
//                "header8" -> TEXT_CARD_HEADER8
//                "footer2" -> TEXT_CARD_FOOTER2
//                "footer3" -> TEXT_CARD_FOOTER3
//                else -> UNKNOWN
//            }
//        }
        "banner" -> {
            when (dataType) {
                "Banner" -> BANNER
                else -> UNKNOWN
            }
        }
        "banner3" -> {
            when (dataType) {
                "Banner" -> BANNER3
                else -> UNKNOWN
            }
        }
        "videoSmallCard" -> {
            when (dataType) {
                "VideoBeanForClient" -> VIDEO_SMALL_CARD
                else -> UNKNOWN
            }
        }
        "briefCard" -> {
            when (dataType) {
                "TagBriefCard" -> TAG_BRIEFCARD
                "TopicBriefCard" -> TOPIC_BRIEFCARD
                else -> UNKNOWN
            }
        }
        "followCard" -> {
            when (dataType) {
                "FollowCard" -> FOLLOW_CARD
                else -> UNKNOWN
            }
        }
        "informationCard" -> {
            when (dataType) {
                "InformationCard" -> INFORMATION_CARD
                else -> UNKNOWN
            }
        }
        "ugcSelectedCardCollection" -> {
            when (dataType) {
                "ItemCollection" -> UGC_SELECTED_CARD_COLLECTION
                else -> UNKNOWN
            }
        }
        "autoPlayVideoAd" -> {
            when (dataType) {
                "AutoPlayVideoAdDetail" -> AUTO_PLAY_VIDEO_AD
                else -> UNKNOWN
            }
        }
        else -> UNKNOWN
    }

    private fun getTextCardType(type: String?): Int = when (type) {
        "header5" -> TEXT_CARD_HEADER5
        "header7" -> TEXT_CARD_HEADER7
        "header8" -> TEXT_CARD_HEADER8
        "footer2" -> TEXT_CARD_FOOTER2
        "footer3" -> TEXT_CARD_FOOTER3
        else -> UNKNOWN
    }

    abstract class AbstractCommendViewHolder(val binding: ViewDataBinding) :
        RecyclerView.ViewHolder(binding.root)

    class EmptyViewHolder(view: View) : RecyclerView.ViewHolder(view)

    class TextCardViewHeader5ViewHolder(binding: ItemTextCardTypeHeaderFiveBinding) :
        AbstractCommendViewHolder(binding)

    class SpecialSquareCardCollectionViewHolder(binding: ItemSpecialSquareCardCollectionTypeBinding) :
        AbstractCommendViewHolder(binding)

    class FollowCardViewHolder(binding: ItemFollowCardTypeBinding) :
        AbstractCommendViewHolder(binding)

    class VideoSmallCardViewHolder(binding: ItemVideoSmallCardTypeBinding) :
        AbstractCommendViewHolder(binding)
}